package com.gitee.swsk33.kafkasender.task;


import com.gitee.swsk33.kafkaentity.dataobject.Precipitation;
import jakarta.annotation.Resource;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Random;

@Component
public class SendTask {

	@Resource
	private KafkaTemplate<String, Precipitation> kafkaTemplate;

	/**
	 * 定时任务：每一秒发送一次数据
	 */
	@Scheduled(fixedDelay = 1000)
	public void send() {
		// 产生随机数据
		Precipitation current = new Precipitation();
		Random random = new Random();
		current.setValue(random.nextInt(100));
		current.setGmtCreated(LocalDateTime.now());
		current.setGmtModified(LocalDateTime.now());
		// 发送给weather话题
		kafkaTemplate.send("weather", current);
		System.out.println("已完成发送：" + current);
	}

}